import { PLATFORM_SEND_TYPE, PLATFORM_TYPE, PLARFORM_FILM_TYPE } from "../../constant/platform";
import { FilmMultimediaRelation } from "../../entities/filmMultimediaRelation";
import { checkDataOk, genErrorData, genSuccessData } from "../../response";

export class FilmReleaseController {
  constructor(opt) {
    this.proxyController = opt.proxyController;
    this.filmReleaseService = opt.filmReleaseService;
    this.requestFactory = opt.requestFactory;
  }

  async qrRelease(c){
    const res = await this.proxyController.request(c.req, '/work_ai/user/filmrelease/create')

    const response = await res.json()

    if(! checkDataOk(response)){
      return c.json(response)
    }

    const { result: id } = response.data

    let result;

    try {
      result = await this.filmReleaseService.getReleaseInfo(id, 20)
    } catch (error) {
      return c.json(genErrorData(error.message))
    }

    if (
      (! [ PLATFORM_TYPE.DOUYIN, PLATFORM_TYPE.WECHAT ].includes(result.platform)) // 判断平台类型
      || result.send_type !== PLATFORM_SEND_TYPE.API // 判断是否是api发布
      || result.film_type !== PLARFORM_FILM_TYPE.VIDEO // 判断是否是视频发布
    ){
      return c.json(response)
    }

    try {
      // 新增发布
      await this.filmReleaseService.addRelease(result)
    } catch (error) {
      return c.json(genErrorData('发布失败'))
    }

    return c.json(genSuccessData({
      result: id
    }))
  }

  async rePublish(c){
    const body = await c.req.json()
    let mid = body.mid;

    if(body.id){
      let res = await FilmMultimediaRelation.findOne({
        where: {
          id: body.id
        }
      })

      if(! res){
        let detail = await this.filmReleaseService.getFilmMultiMediaDetail(body.id)

        if(! checkDataOk(detail)){
          return c.json(genErrorData('未找到该发布任务'))
        }

        mid = detail.data.result.film_release_id
      } else {
        mid = res.film_release_id
      }
    }

    if(! mid){
      return c.json(genErrorData('参数错误'))
    }

    let response;

    try {
      response = await this.requestFactory.request({
        url: '/work_ai/user/filmrelease/rePublish',
        method: 'PUT',
        headers: {
          'Content-Type': 'application/json'
        },
        data: body
      })
    } catch (error) {
      return c.json(genErrorData(error.message))
    }

    if(! checkDataOk(response)){
      return c.json(response)
    }

    let result;

    try {
      result = await this.filmReleaseService.getReleaseInfo(mid, 999999)
    } catch (error) {
      return c.json(genErrorData(error.message))
    }

    if (
      (! [ PLATFORM_TYPE.DOUYIN, PLATFORM_TYPE.WECHAT ].includes(result.platform)) // 判断平台类型
      || result.send_type !== PLATFORM_SEND_TYPE.API // 判断是否是api发布
      || result.film_type !== PLARFORM_FILM_TYPE.VIDEO // 判断是否是视频发布
    ){
      return c.json(response)
    }

    try {
      // 更新发布
      await this.filmReleaseService.updateRelease(result, body.id || null)
    } catch (error) {
      return c.json(genErrorData('发布失败'))
    }

    return c.json(genSuccessData({
      result: 'success'
    }))
  }
}